﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Medianamik.Core.Urls;
using System.Web;
using System.Collections.Specialized;

namespace Medianamik.Core.Redirect
{
    public class RedirectDictionaryEntry 
    {
        public RedirectDictionaryEntry(string pPath, int pResponseCode, IUrlDictionaryEntry pUrlDicoEntry, string pAppendQS)
        {
            RedirectFromPath = pPath.ToLowerInvariant();
            ResponseCode = pResponseCode;
            UrlDictionaryEntry = pUrlDicoEntry;
            if (!string.IsNullOrEmpty(pAppendQS))
            {
                QueryString = HttpUtility.ParseQueryString('?' + pAppendQS.TrimStart('?'));
            }
            else
            {
                QueryString = new NameValueCollection();
            }
                        
        }

        public int ResponseCode
        {
            get;
            private set;
        }

        public string RedirectFromPath
        {
            get;
            private set;
        }

        private Uri _redirectFromUri;
        public Uri RedirectFromUri
        {
            get
            {
                return _redirectFromUri
                 ?? (_redirectFromUri = new Uri(UrlDictionaryEntry.RequestedUrl, RedirectFromPath));
            }
        }

        public IUrlDictionaryEntry UrlDictionaryEntry
        {
            get;private set;
        }

        public NameValueCollection QueryString
        {
            get;
            private set;
        }
    }
}
